package com.czr.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czr.admin.pojo.Users;
import com.czr.admin.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.Serializable;

@Controller
public class UsersController {

    @Autowired
    private UsersService usersService;

    /**
     * 分页查询
     * @param pageNum 当前页
     * @param pageSize 页面大小
     * @param model
     * @return
     */
    @RequestMapping("/usersList")
    public String usersList(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                            @RequestParam(value = "pageSize",defaultValue = "5") int pageSize,
                            Users users,
                            Model model){
        System.out.println("usersList:" + users);
        Page<Users> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Users> wrapper = new QueryWrapper<>();

        //判断各参数是否有值，有值，把值加在wrapper中
        if (!StringUtils.isEmpty(users.getUname())) {
            wrapper.like("uname", users.getUname());
        }
        if (!StringUtils.isEmpty(users.getUsex())) {
            wrapper.like("usex", users.getUsex());
        }
        if (!StringUtils.isEmpty(users.getUaddress())) {
            wrapper.like("uaddress", users.getUaddress());
        }

        usersService.page(page, wrapper);
        System.out.println(page.getPages());
        model.addAttribute("page", page);
        // 信息回填  提升体验
        model.addAttribute("users", users);
        return "/admin/member";
    }

    /**
     * 回写 修改数据
     * @param id    获取id
     * @param model
     * @return
     */
    @RequestMapping("/tomemberEdit/{id}")
    public String editUser(@PathVariable("id") long id,
                           Model model){

        System.out.println(id);
        Users userbyId = usersService.getById(id);
        model.addAttribute("userbyId", userbyId);
        return "/admin/memberEdit";
    }

    /**
     * 获取修改的用户
     * @param users
     * @return   修改成功后返回分页
     */
    @RequestMapping("/updMember")
    public String updMember(Users users){
        usersService.updateById(users);
        return "redirect:/usersList";
    }

    /**
     * 根据id删除用户
     * @param id
     * @return   删除成功后返回分页
     */
    @RequestMapping("/tomemberDel/{id}")
    public String delUser(@PathVariable("id") Integer id,
                          @RequestParam(value = "pn",defaultValue = "1") Integer pageNum,
                          RedirectAttributes ra // 重定向携带参数
    ){
        System.out.println(id);
        usersService.removeById(id);

        ra.addAttribute("pageNum", pageNum);
        return "redirect:/usersList";
    }

    /**
     * 增加一条数据
     * @param users
     * @return  增加成功后返回分页
     */
    @RequestMapping("/addMember")
    public String toAddMember(Users users){
        usersService.save(users);
        return "redirect:/usersList";
    }



}
